from pydantic import BaseModel, Field
from datetime import datetime
from enum import Enum
from typing import Optional


class UserRole(str, Enum):
    STUDENT = "student"
    TEACHER = "teacher"
    ADMIN = "admin"


# 新增：创建用户模型（不需要id和created_at，由数据库自动生成）
class CreateUser(BaseModel):
    username: str = Field(..., description="用户名（唯一）", min_length=1, max_length=100)
    real_name: str = Field(None, description="真实姓名", max_length=100)
    role: UserRole = Field(default=UserRole.STUDENT, description="用户角色")


# 新增：更新用户模型（所有字段可选，支持部分更新）
class UpdateUser(BaseModel):
    username: str = Field(None, description="用户名（唯一）", min_length=1, max_length=100)
    real_name: str = Field(None, description="真实姓名", max_length=100)
    role: UserRole = Field(None, description="用户角色")


# 原用户信息模型（用于查询返回，补充datetime类型）
class UserInfo(BaseModel):
    id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    real_name: Optional[str] = Field(None, description="真实姓名")
    role: UserRole = Field(..., description="用户角色")
    created_at: datetime = Field(..., description="创建时间")

    class Config:
        from_attributes = True  # 支持从SQLAlchemy模型转换